;;
;; bitchin - the GUI toolkit that uses the web as a back end.
;;

;;
;; Unit tests
;;
;; Just (require 'org.mired.bitchin.test) to run these. To run them repeatedly
;; from the REPL, first do (use ['clojure.test :only ['run-tests]]), and then
;; you can do (run-tests 'org.mired.bitchin.test) to run the tests repeatedly.
;; This is dangerous as it may not reload the code you're testing. Running it
;; from the command line is safer. Just giving test.clj as an argument to
;; clojure.main should be sufficient for clojure 1.1 and later.

(ns org.mired.bitchin.test
  [:use [clojure.test :only [deftest is]] [org.mired.bitchin.html]])

(deftest inline-elements
  (is (= "<p />" (p)) "empty contents")
  (is (= "<map />" (map_)) "keyword tag")
  (is (= "<p>now</p>" (p "now")) "string contents")
  (is (= "<p>now</p>" (p 'now)) "symbol contents")
  (is (= "<p>:now</p>" (p :now)) "keyword contents")
  (is (= "<p><br /></p>" (p (br))) "element content")
;  (is (= "<p>now &amp;</p>" (p "now &")) "ampersand encoding")
;  (is (= "<p>now &lt;</p>" (p "now <")) "less than encoding")
  (is (= "<p>now is the</p>" (p "now " 'is " the")) "multipart contents"))

(deftest block-elements
  (is (= "<div />" (div)) "empty contents")
  (is (= "<meta />" (meta_)) "keyword tag")
  (is (= "<div>\nthen\n</div>" (div "then")) "string contents")
  (is (= "<div>\nthen\n</div>" (div 'then)) "symbol contents")
  (is (= "<div>\n:then\n</div>" (div :then)) "keyword contents")
  (is (= "<div>\n<br />\n</div>" (div (br))) "element contents")
;  (is (= "<p>now &amp;</p>" (p "now &")) "ampersand encoding")
;  (is (= "<p>now &lt;</p>" (p "now <")) "less than encoding")
  (is (= "<div>\nthen\nwas\nthe\n</div>" (div "then" 'was "the"))
      "multipart contents"))

(deftest mixed-content
  (is (= "<div>\n<span>now is the</span>\n<b>time</b>\n</div>"
	 (div (span "now is the") (b 'time)))))

(deftest attributes-block
  (is (= "<div border=\"0\" />" (div {:border 0})) "keyword name")
  (is (= "<div border=\"0\" />" (div {'border 0})) "symbol name")
  (is (= "<div border=\"0\" />" (div {"border" 0})) "string name")
  (is (= "<div width=\"100%\" />" (div {:width "100%"})) "string value")
  (is (= "<div border=\"0\" width=\"100%\" />" (div {:border 0, 'width "100%"}))
      "multiple attributes")
  (is (= "<div border=\"0\" width=\"100%\">\n<span width=\"90%\">Text</span>\n</div>"
	 (div {"border" 0, :width "100%"} (span {'width "90%"} "Text")))
      "multiple attributes with content"))

(deftest attributes-inline
  (is (= "<span border=\"0\" />" (span {:border 0})) "keyword name")
  (is (= "<span border=\"0\" />" (span {'border 0})) "symbol name")
  (is (= "<span border=\"0\" />" (span {"border" 0})) "string name")
  (is (= "<span width=\"100%\" />" (span {:width "100%"})) "string value")
  (is (= "<span border=\"0\" width=\"100%\" />" (span {:border 0, 'width "100%"}))
      "multiple attributes")
  (is (= "<span class=\"now&amp;then\" />" (span {:class "now&then"})) 
      "ampersand encoding")
  (is (= "<span class=\"&quot;FOO&quot;\" />" (span {:class "\"FOO\""}))
	 "quote encoding")
  (is (= "<span class=\"x&gt;3\" />" (span {:class "x>3"}))
      "greater than encoding")
  (is (= "<span border=\"0\" width=\"100%\"><b width=\"90%\">Text</b></span>"
	 (span {"border" 0, :width "100%"} (b {'width "90%"} "Text")))
      "multiple attributes with content"))

; To run these after requireing org.mired.bitchin.test, use:
(clojure.test/run-tests 'org.mired.bitchin.test)
